package no.hio.wsprovider.services;

import java.util.List;
import java.util.NoSuchElementException;

import javax.jws.WebService;

import no.hio.wsprovider.dao.ProductDAO;
import no.hio.wsprovider.vo.Product;

@WebService(name="ProductService", targetNamespace="http://hio.no/wsproviderproduct")
public class ProductServiceImpl implements ProductService {

	ProductDAO productDAO;
	

	/**
	 * @return the productDAO
	 */
	public ProductDAO getProductDAO() {
		return productDAO;
	}

	/* (non-Javadoc)
	 * @see no.hio.wsprovider.services.ProductService#insertProduct(no.hio.wsprovider.vo.Product)
	 */
	@Override
	public int insertProduct(Product product) {
		try{
			return productDAO.insert(product);
		}
		catch(UnsupportedOperationException e)
		{
			return -1;
		}
	}

	/* (non-Javadoc)
	 * @see no.hio.wsprovider.services.ProductService#updateProduct(no.hio.wsprovider.vo.Product)
	 */
	@Override
	public void updateProduct(Product product) {
		//TODO: Log noe her!
			try {
				productDAO.update(product);
			} catch (NoSuchElementException e) {
				//e.printStackTrace();
			}
	}

	/**
	 * @param productDAO the productDAO to set
	 */
	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	/* (non-Javadoc)
	 * @see no.hio.wsprovider.services.ProductService#findAllProducts()
	 */
	@Override
	public List<Product> findAllProducts() {
		
		return productDAO.findAll();
	}

	/* (non-Javadoc)
	 * @see no.hio.wsprovider.services.ProductService#findByProductID(int)
	 */
	@Override
	public Product findByProductID(int productID) {
		
		return productDAO.findByProductID(productID);
		
	}

	/* (non-Javadoc)
	 * @see no.hio.wsprovider.services.ProductService#removeProduct(no.hio.wsprovider.vo.Product)
	 */
	@Override
	public void removeProduct(Product product) {
		
		try {
			productDAO.remove(product);
		} catch (NoSuchElementException e) {
			//TODO: logg?
		}
	}


}
